package org.example.ychdp.controller;

import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import org.example.ychdp.entity.Concert;
import org.example.ychdp.entity.User;
import org.example.ychdp.service.ConcertService;
import org.example.ychdp.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/concert")
@Controller
public class adminConcertController {

    @Autowired
    private ConcertService concertService;

    @RequestMapping(value = "/concertManage")
    public ModelAndView testUserManage(HttpSession session,
                                       @RequestParam(required = false, defaultValue = "1") Integer page,
                                       @RequestParam(required = false, defaultValue = "") String nickname,
                                       @RequestParam(required = false, defaultValue = "") String number) {
        if (session.getAttribute("admin")==null){
            Map<String,Object> model = new HashMap<String,Object>();
            String errorMsg = "您未登录，请登录";
            model.put("errorMsg",errorMsg);
            ModelAndView modelAndView = new ModelAndView("admin/adminLogin",model);
            return modelAndView;
        }
        else {
//            List<Concert> userList = concertService.selectAllConcert();
            PageInfo<Concert> userSearchResult = concertService.searchConcert(page, nickname, number);
            Map<String,Object> model = new HashMap<String,Object>();
            model.put("activeMenuItem", "concertManage");
            model.put("userL",userSearchResult);
            ModelAndView modelAndView = new ModelAndView("admin/Concert/concertManage",model);
            return modelAndView;
        }

    }

    @RequestMapping("/addConcert")
    public String toInsertPage() {
        return "admin/Concert/addConcert";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addStudent(Concert concert) {
        int i = concertService.insertConcert(concert);
        return "redirect:concertManage";
    }

    @RequestMapping("/deleteConcert")
    public String deleteUser(@RequestParam("id") int id)
    {
        int i = concertService.deleteConcertById(id);
        return "redirect:concertManage";
    }

    @RequestMapping(value = "/editConcert", method = RequestMethod.GET)
    public String editUser(@RequestParam("id") int id, Model model) {
        Concert concert = concertService.getConcertById(id);
        model.addAttribute("concert", concert);
        return "admin/Concert/editConcert";
    }

    @RequestMapping(value = "/updateConcert", method = RequestMethod.POST)
    public String updateUser(Concert concert) {
        concertService.updateUser(concert);
        return "redirect:concertManage";
    }
}
